using Crux.Core.Websockets;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace System;
/// <summary>
/// WebSocketServiceCollectionExtensions
/// </summary>
public static class WebSocketServiceCollectionExtensions
{
    /// <summary>
    ///
    /// </summary>
    public static IServiceCollection AddWebSocketHandle<THandler>(
        this IServiceCollection services)
        where THandler : WebSocketHandler
    {
        services.TryAddSingleton(typeof(WebSocketDispatcher<>));
        services.TryAddSingleton(typeof(WebSocketConnectionHandler<>));

        return services;
    }

    /// <summary>
    ///
    /// </summary>
    public static IServiceCollection ConfigureWebSocketOptions<THandler>(
        this IServiceCollection services,
        Action<WebSocketOptions<THandler>> configureOptions)
        where THandler : WebSocketHandler
    {
        _ = services.Configure(configureOptions);

        return services;
    }

    /// <summary>
    ///
    /// </summary>
    public static IServiceCollection AddLocalSupport(this IServiceCollection services)
    {
        services.TryAddSingleton(typeof(WebSocketLifetimeManager<>), typeof(LocalLifetimeManager<>));
        return services;
    }
}